@echo off
setlocal
set _SCRIPTDIR=%~dp0foobar\
set _SCRIPTDIR=%_SCRIPTDIR:\foobar\=%

set _PROJDIR=%_SCRIPTDIR%

set _TESTDIR=%_PROJDIR%\src\xpi\chrome\content\test

set _PROFILEDIR=%HOMEDRIVE%%HOMEPATH%\Application Data\Mozilla\Firefox\Profiles
set _FIREFOXEXE=C:\Program Files\Mozilla Firefox\firefox.exe

rem Find the requested command.
for /F %%v in ('echo %1^|findstr "^runjar$ ^rhino$ ^testgen$ ^xpidl$ ^zap$ ^mgr$ ^run"') do call :exec set COMMAND=%%v

if "%COMMAND%" == "" (
    echo Usage: %0 { runjar : zapmoz : runmoz }
    pause
    goto :eof
) else (
    shift
)

call :%COMMAND% %1 %2 %3 %4 %5 %6 %7 %8 %9
rem if errorlevel 1 pause
goto :eof

:runjar
rem call :gpgsetup alice
gpg "E:\ipsumenc.asc"
rem java -jar "%_PROJDIR%\build\tst\lucidity.jar" -d "E:\ipsumenc.asc" "%HOMEDRIVE%%HOMEPATH%\Application Data\gnupg\secring.gpg" %*
goto :eof

:rhino
java -jar "%_PROJDIR%\build\tst\lucidity.jar" rhino %*
goto :eof

:resetdevx
if not exist "%_PROFILEDIR%\%1" (
    "%_FIREFOXEXE%" -createprofile "%1 %_PROFILEDIR%\%1"
)
rd /q /s "%_PROFILEDIR%\%1"
mkdir "%_PROFILEDIR%\%1\extensions"
echo/%_PROJDIR%\src\xpi> "%_PROFILEDIR%\%1\extensions\lucidity@emergent.org"
goto :eof

:rundevx
if not exist "%_PROFILEDIR%\%1" (
    call :resetdevx %1
)
start "ff" "%_FIREFOXEXE%" -console -P %1 -no-remote http://www.google.com/ig
rem http://gmail.com
rem "%_PROJDIR%\src\xpi\chrome\content\modules\test\test.html"
goto :eof

:zap
call :resetdevx debug
goto :eof

:run
call :rundevx debug
goto :eof

:mgr
"%_FIREFOXEXE%" -profilemanager
goto :eof

:xpidl
set GECKOSDKHOME=C:\apps\gecko-sdk
set _SDKDIR=%GECKOSDKHOME%
"%_SDKDIR%\bin\xpidl.exe" -m typelib -w -v -I "%_SDKDIR%\idl" -o "%_PROJDIR%\src\xpi\components\LucidService" "%_PROJDIR%\src\xpi\components\LucidService.idl"
goto :eof

:gpgsetup
set _KSHOME=%_TESTDIR%\%1
set GNUPGHOME=%_KSHOME%
goto :eof

:: %1 user
:gpggenkey
del /q "%_KSHOME%\*.gpg"
del /q "%_KSHOME%\*.bak"
del /q "%_KSHOME%\random_seed"
gpg --no-tty --s2k-cipher-algo AES --s2k-digest-algo SHA1 --s2k-mode 3 --gen-key --batch "%_KSHOME%\keygen.txt"
gpg -a --export-secret-key > "%_KSHOME%\sec-keys.asc"
gpg -a --export > "%_KSHOME%\pub-keys.asc"
goto :eof

:: %1 user
:: %2 targetuser
:: %3 passphrase
:: %4 outfile
:gpgencryptsign
del %4
echo/%3|gpg -u "%1" -r "%2" --armour --passphrase-fd 0 %5 %6 %7 %8 %9 ^
 --trust-model always --output %4 ^
 --sign --encrypt "%_TESTDIR%\loremipsum.txt"
goto :eof

:testgen
call :gpgsetup userA
call :gpggenkey
call :gpgencryptsign alice@sprockets.com alice@sprockets.org asdf "%_KSHOME%\text1-com2org-es.asc"
call :gpgencryptsign alice@sprockets.org alice@sprockets.com asdf "%_KSHOME%\text1-org2com-es.asc"
del /q "%_KSHOME%\*.bak"
del /q "%_KSHOME%\random_seed"
goto :eof

:exec
%*
goto :eof

